////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.display.windows.objects {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormatAlign;
	
	import kiwi.components.Checkbox;
	import kiwi.events.CallbackEvent;
	
	import plantool.data.Field;
	import plantool.data.FieldTypes;
	import plantool.display.windows.BaseWindow;
	import plantool.helpers.TextFieldHelper;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	[Event(name="complete", type="flash.events.Event")]
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class WindowFieldRow extends WindowRow {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _field:Field		= null;
		private var _data:*				= null;
		private var _label:String		= "";
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		public function get data():* {
			return _data;
		}
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function WindowFieldRow(window:BaseWindow, field:Field, label:String = "") {
			//store the field
			_field = field;
			
			//set label if not provided
			if (label == "") _label = _field.name;
			else _label = label;
			
			//super
			super(window);
			
			//override height
			height = 26;
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function onMouse_cb(e:CallbackEvent):void {
			_data = (e.currentTarget as Checkbox).checked;
		}
		
		private function onChange_data(e:Event):void {
			switch (_field.type) {
				default:
					_data = (e.currentTarget as TextField).text;
					break;
			}
		}
		
		protected override function init():void {
			//call super init
			super.init();
			
			//always add a label
			var tf:TextField 	= TextFieldHelper.createInfoTf(TextFieldAutoSize.LEFT, TextFormatAlign.LEFT, false, false, 0);
			tf.x				= 5;
			tf.y				= (height - tf.height) / 2;
			tf.text				= _label;
			tf.width			= (width - 10) / 2;
			addChild(tf);
			
			//add content based on field type
			switch (_field.type) {
				case FieldTypes.BOOLEAN:
					var cb:Checkbox = new Checkbox();
					cb.width			= height - 8;
					cb.height			= height - 8;
					cb.x				= width / 2;
					cb.y				= (height - cb.height) / 2;
					cb.mouseHandler		= onMouse_cb;
					addChild(cb);
					
					_data = false
					break;
				case FieldTypes.TEXT:
				default:
					//create input field
					var input:TextField = TextFieldHelper.createInputTf(false);
					input.x				= width / 2;
					input.y				= (height - tf.height) / 2;
					input.width			= (width - 20) / 2;
					input.text			= "";
					input.addEventListener(Event.CHANGE		, onChange_data, false, 0, true);
					addChild(input);
					
					//set data
					_data = "";
					break;
			}
		}
	}
}